import java.io.*;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Lock;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*;
import java.rmi.registry.Registry;
import java.rmi.RemoteException;

class broker {

	private static String f = "config.ini";//"C:\\Users\\Chris\\Documents\\NetBeansProjects\\OsProject4\\src\\config.ini";//

	private static Hashtable<String, String> config;

	private static Lock dbLock = new ReentrantLock();

	private static Hashtable<String, Hashtable<String, String>> db;

	public static void main(String[] args) {

		boolean running = true;
		
		Thread serverThread = null;
		
		//while (running) 
		{

			try {

				//load the config file
				loadConfig(f);

				//if the config file is missing the db file, make one
				if (!config.containsKey("DATABASE")) {
					config.put("DATABASE", "db.dat");
				}

				//load data store
				db = DbStorage.load((String) config.get("DATABASE"));
				
				rmibroker s = new LookupProvider(broker.dbLock, broker.db);

				System.out.print("Exporting server object");
				rmibroker brk = (rmibroker) UnicastRemoteObject.exportObject(s,0);
				System.out.println("...done");

				//Register the object with the RMI registry
				String port = (String) config.get("LOOKUPPORT");
				Integer p = -1;
				if (port != null)
					p = Integer.parseInt(port);
				else
					p = 4337;

				Registry registry = LocateRegistry.createRegistry(p);

				System.out.print("Binding server object to port");
				registry.rebind((String) config.get("BIND-NAME"), brk);
				System.out.println("...done");

				BufferedReader reader = new BufferedReader(
						new InputStreamReader(System.in));
				String line = "";

				//start the service provider registration server
				ServiceProviderServer serv = new ServiceProviderServer(db, config, dbLock);
				
				serverThread = new Thread(serv);
				
				serverThread.start();

				do {
					line = reader.readLine();
				} while (!line.equalsIgnoreCase("exit"));

				reader.close();
				
				System.out.print("Removing registry entry");
				registry.unbind((String) config.get("BIND-NAME"));
				System.out.println("...done");

				running = false;
				
				serverThread.interrupt();

			} catch (Exception e) {

				System.out.println(msg.E4 + " " + e.getMessage() + "\n\n");
				serverThread.interrupt();
			}
		}

	}

	public broker() throws RemoteException {}

	
	private static void loadConfig(String f) {

		config = new Hashtable<String, String>();

		try {
			BufferedReader r = new BufferedReader(new FileReader(f));

			//read config file
			String line = "";

			while ((line = r.readLine()) != null) {

				//Don't add blank lines to hash
				if (line.equals("")) {
					continue;
				}

				String[] items = line.split("=");

				config.put(items[0].toUpperCase(), items[1]);

			}

			r.close();
		} catch (FileNotFoundException e) {

			System.out.println(msg.E4 + " " + e.getMessage());

			System.out.println("Config file not found, using default values");
			config.put("BIND-NAME", "cshields");
			config.put("DATABASE", "db.dat");

			return;
		} catch (IOException e) {

			System.out.println(msg.E4 + " " + e.getMessage());

			return;
		}

		//if the config file is missing the db file, make one
		if (!config.containsKey("DATABASE")) {
			config.put("DATABASE", "db.dat");
		}

	}
	
}
